<template>
  <div id="box">
    <div id="header">
      <!-- 主题 -->
      <div id="subjectDiv">
        <span :tabindex='0' @click="getList('')">全部</span>
        <span :tabindex='111' v-show='!isAll' style="color: #67a1e5">{{subject.name}}</span>
        <span> |</span>
        <span v-for="item in subjectList" :key='item.subject_id' :tabindex="item.subject_id" @click="getList(item)">
          {{item.name}}
        </span>
      </div>
      <!-- 发布文章 -->
      <el-button id="editorBtn" @click="to_editor" type="info" size="small">
        <i class='el-icon-circle-plus-outline' /> 发布
      </el-button>
    </div>
    <!-- 文章列表 -->
    <div id='content' class="content">
      <!-- 回到顶部 -->
      <el-backtop target=".content" :visibility-height="200"></el-backtop>
      <div id="article-list">
        <!-- 游记列表... -->
        <div :class="{article_item: true, with_cover: item.cover != null}" v-for="item in list" :key="item.article_id">
          <div class="title" @click="to_detail(item)">{{item.title}}</div>
          <div class="abstract">{{item.abstract}}</div>
          <div class="info">
            <span class="user-name">{{item.user_all.user_name}}</span>
            <div class="footer-divider"></div>
            <span class="view">
              <i class="iconfont icon-liulan" />
              {{item.views}}
            </span>
            <span :class="{like: true, active_color: item.isLike}">
              <i class="iconfont icon-dianzan-line" @click="likeChange(item)" />
              {{item.num_like.num}}
            </span>
            <span :class="{collect: true, active_color:item.isCollect}">
              <i class="iconfont icon-shoucang-line" @click="collectChange(item)" />
              {{item.num_collect.num}}
            </span>
            <span class="subject" v-if="item.article_subject != null">{{item.article_subject.name}}</span>
          </div>
          <div class="cover" v-if="item.cover != null">
            <img :src="item.cover" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import likeApi from "@/api/like.js";
import collectApi from "@/api/collect.js";
import articleApi from '@/api/article.js'
export default {
  data() {
    return {
      list: [], //文章列表
      subjectList: [], //主题列表
      subject: '', //所选主题
      isAll: true, //是否展示全部主题
    }
  },
  mounted() {
    this.getSubject();
    this.getList();
  },
  methods: {
    //获取文章主题列表
    async getSubject() {
      this.subjectList=(await articleApi.getSubject()).data;
    },

    //获取全部游记列表
    async getList(item) {
      if(item!=null) this.subject=item;
      if(this.subject=='') {
        //全部主题
        this.list=await (await articleApi.getList(0)).data
        this.$nextTick(() => {
          const first=document.querySelector("#subjectDiv span:nth-child(1)")
          first.style.color="#67a1e5";
        })
        this.isAll=true
      }
      else {
        this.list=await (await articleApi.getList(0,this.subject.subject_id)).data
        this.$nextTick(() => {
          const first=document.querySelector("#subjectDiv span:nth-child(1)")
          first.style.color='#d3d6dd';
        })
        this.isAll=false
      }
    },

    //跳转游记创作界面
    to_editor() {
      this.$router.push("/scenery/editor")
    },

    //跳转到游记详情界面
    to_detail(item) {
      this.$router.push({
        name: '景区服务-游记详情',
        params: {
          article_id: item.article_id
        }
      })
    },

    //当前登录用户是否已点赞
    isLike(item) {
      const res=likeApi.queryLike(item.article_id,1)
      return res.data
    },

    //当前登录用户是否已收藏
    isCollect(item) {
      const res=collectApi.queryCollect(item.article_id,1)
      return res.data
    },

    //切换点赞状态
    async likeChange(item) {
      const res=await likeApi.changeLike(item.article_id,1)
      item.num_like.num=res.data
      item.isLike=!item.isLike
    },

    //切换收藏状态
    async collectChange(item) {
      const res=await collectApi.changeCollect(item.article_id,1)
      item.num_collect.num=res.data
      item.isCollect=!item.isCollect
    },
  }
}
</script>

<style lang="scss" scoped>
#box {
  position: relative;
  background-color: rgba(34, 52, 87, 0.6);
}

#header {
  padding: 4px 460px 0 460px;
}

#subjectDiv {
  position: absolute;
  height: 35px;
  overflow: hidden;
  max-width: 770px;
  margin-top: 12px;
  padding: 4px 12px;
  border-radius: 4px;
  user-select: none;
  color: #b0c4de;
  background-color: rgba(34, 52, 87, 0.3);
  &:hover {
    height: auto;
    overflow: auto;
  }
  span {
    display: inline-block;
    margin-right: 10px;
    font-size: 18px;
    line-height: 27px;
    cursor: pointer;
    &:hover {
      color: rgba(26, 143, 204, 1);
    }
    &:active {
      color: #67a1e5;
    }
  }
}

#editorBtn {
  position: absolute;
  top: 14px;
  right: 588px;
  font-size: 18px;
  border-radius: 8px;
  color: #d7d7d7;
  text-shadow: 0 0 40px #67a1e5;
  background-color: rgba(26, 143, 204, 1);
  &:hover {
    background-color: rgba(93, 161, 229, 1);
  }
}

#content { 
  width: 100%;
  height: 90%;
  padding: 4px 460px;
  margin-top: 80px;
  overflow-y: auto;
  overflow-x: hidden;
  &::-webkit-scrollbar {
    width: 8px;
  }
  &::-webkit-scrollbar-thumb {
    background-color: #1f3964;
    border-radius: 8px;
  }
  ::v-deep .el-backtop {
    user-select: none;
  }
}

#article-list {
  min-height: 100%;
  width: 880px; 
  padding: 16px 32px;
  background-color: rgba(34, 52, 87, 0.4);
  border-radius: 8px;
  user-select: none;
  cursor: pointer;
}

.article_item {
  position: relative;
  height: 120px;
  padding: 16px 20px 0;
  font-family: -apple-system, system-ui, Segoe UI, Roboto, Ubuntu, Cantarell,
    Noto Sans, sans-serif, BlinkMacSystemFont, Helvetica Neue, PingFang SC,
    Hiragino Sans GB, Microsoft YaHei, Arial !important;
  .title {
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    margin-bottom: 4px;
    font-weight: 500;
    font-size: 22px;
    line-height: 33px;
    letter-spacing: 1.5px;
    color: rgba(26, 143, 204, 1);
  }
  .abstract {
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    font-weight: 400;
    font-size: 18px;
    line-height: 30px;
    color: #b0c4de;
  }
  .info {
    font-weight: 400;
    font-size: 18px;
    line-height: 27px;
    color: #b0c4de;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
    padding-bottom: 12px;
    border-bottom: 1px solid rgba(228, 230, 235, 0.5);
    .user-name {
      color: #5fa6c3;
    }
    .footer-divider {
      display: inline-block;
      width: 1px;
      height: 12px;
      background-color: #e4e6eb;
      margin: 4px 8px 0 8px;
    }
    .like {
      margin-left: 4px;
    }
    .collect {
      margin-left: 4px;
    }
    .subject {
      float: right;
      color: rgba(209, 215, 221, 0.8);
      letter-spacing: 1px;
      background-color: rgba(0, 151, 215, 0.8);
      padding: 0 6px;
      margin-top: 2px;
      border-radius: 2px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      font-size: 14px;
      min-height: 21px;
      line-height: 21px;
    }
  }
  .cover {
    position: absolute;
    top: 20px;
    right: 20px;
    img {
      display: inline;
      flex: 0 0 auto;
      width: 114px;
      height: 90px;
      margin-left: 24px;
      background-color: #fff;
      border-radius: 4px;
      border: 1px solid rgba(26, 160, 221, 0.5);
      //overflow-clip-margin: content-box;
      overflow: clip;
      -o-object-fit: cover;
      object-fit: cover;
    }
  }
}
.with_cover {
  .title,
  .abstract,
  .info {
    padding-right: 140px !important;
  }
}
.active_color {
  color: #5fa6c3;
}
</style>